package com.dianzi.certification.controller;

import com.dianzi.certification.dto.ProfessionSelfEvaluationDTO;
import com.dianzi.certification.entity.ProfessionSelfEvaluation;
import com.dianzi.certification.service.ProfessionSelfEvaluationService;
import com.dianzi.certification.utils.JwtUtils;
import com.dianzi.certification.vo.ProfessionSelfEvaluationVO;
import com.dianzi.certification.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-03-29 21:16
 * @function
 */
@RestController
@Api(tags = {"流程：专业自评"})
public class ProfessionSelfEvaluationController {

    @Resource
    private ProfessionSelfEvaluationService professionSelfEvaluationService;

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PostMapping("pro")
    @ApiOperation(value = "新增指标")
    public R<String> addPro(@RequestBody ProfessionSelfEvaluationDTO professionSelfEvaluationDTO,
                             HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return professionSelfEvaluationService.addPro(professionSelfEvaluationDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("pro")
    @ApiOperation(value = "查看专业自评")
    public R<List<ProfessionSelfEvaluationVO>> getPro(HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return professionSelfEvaluationService.getPro(userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("pro/{proId}")
    @ApiOperation(value = "查看专业自评详情")
    public R<ProfessionSelfEvaluation> getProDetail(@PathVariable("proId") Integer proId,
                                              HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return professionSelfEvaluationService.getProDetail(proId, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PutMapping("pro")
    @ApiOperation(value = "修改指标")
    public R<ProfessionSelfEvaluation> putPro(@RequestBody ProfessionSelfEvaluationDTO professionSelfEvaluationDTO,
                                                    HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return professionSelfEvaluationService.putPro(professionSelfEvaluationDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @GetMapping("admin/proDetail/{certId}")
    @ApiOperation(value = "查看详细认证（认证专家）")
    public R<ProfessionSelfEvaluation> getProDetailAdmin(@PathVariable Integer certId) {
        return professionSelfEvaluationService.getProDetailAdmin(certId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @PutMapping("admin/pro")
    @ApiOperation(value = "判定认证是否通过：第二阶段")
    public R<String> putProAdmin(@RequestParam @ApiParam("认证id") Integer certId,
                                  @RequestParam @ApiParam("同意-1 驳回-2") Integer status,
                                  @RequestParam(required = false) @ApiParam("若驳回写理由") String remark) {
        return professionSelfEvaluationService.putProAdmin(certId, status, remark);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @PostMapping("admin/pro/expertAdvice")
    @ApiOperation(value = "改进措施处理")
    public R<String> postExpertAdvice(@RequestParam @ApiParam("认证id") Integer certId,
                                      @RequestParam @ApiParam("肯定-1 改进-2") Integer status,
                                      @RequestParam(required = false) @ApiParam("改进措施") String expertAdvice) {
        return professionSelfEvaluationService.postExpertAdvice(certId, status, expertAdvice);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @PutMapping("admin/pro/expertAdvice")
    @ApiOperation(value = "修改改进措施")
    public R<String> putExpertAdvice(@RequestParam @ApiParam("认证id") Integer certId,
                                      @RequestParam() @ApiParam("改进措施") String expertAdvice) {
        return professionSelfEvaluationService.putExpertAdvice(certId, expertAdvice);
    }
}
